/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package EJB.Entity;

import Entity.Usuario;
import Entity.Reserva;
import javax.ejb.Stateless;
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author mquezada
 */
@Stateless
public class UsuarioFacade extends AbstractFacade<Usuario> implements UsuarioFacadeLocal {
    @PersistenceContext(unitName = "Webpizza-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public UsuarioFacade() {
        super(Usuario.class);
    }
    
    public Usuario findByUsername(String username) {
    	Query q = getEntityManager().createQuery("SELECT u FROM Usuario AS u"+
        " WHERE u.usuario = :username");
		q.setParameter("username", username);
		Usuario u = null;
		try {
			u = (Usuario) q.getSingleResult();
		} catch (NoResultException e) { }
		
		return u;
		
    }
    
    @SuppressWarnings("unchecked")
	public List<Reserva> allReservas(int user_id){
        Query q = getEntityManager().createNamedQuery("SELECT object(r) FROM Reserva AS r"+
                " WHERE r.usuario_id = :id");
        q.setParameter("user_id", user_id);
        List<Reserva> resultados = q.getResultList();
        return resultados;
    }

}
